package com.yeban.javase.day12.test.streamtest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/22
 *
 * 4.获取String集合中最长的元素
 */
public class StreamTest04 {

    public static void main(String[] args) {
        List<String> strsList = Arrays.asList("apple", "banana", "cherry", "date", "blueberry", "grape");
        // 获取最长元素的长度
        Integer maxLength = strsList.stream().map(String::length).reduce(Integer::max).orElse(0);
        // 通过最长长度获取对应元素
        strsList.stream().filter(s -> s.length() == maxLength).forEach(System.out::println);

        // 方法2
        Optional<String> str = strsList.stream().max(Comparator.comparingInt(String::length));
        String s = str.orElse("");
        System.out.println(s);

    }
}
